import dayjs from "dayjs";

Page({
  data: {
    // 选择的日期
    minDate: new Date().getTime(),
    maxDate: dayjs().add(2, 'day').valueOf(),//最大日期 2天后
    currentDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    //保修房屋列表
    houseList: [],
    repairItem: [],//报修项目

    attachment: [],//附件
    houseInfo: '',//报修房屋名称
    repairItemName: '',//报修项目名称
    id: '',//报修id
    houseId: '',//已审核通过的房屋id
    mobile: '',//手机号
    appointment: '',//预约时间
    repairItemId: '',//维修项目id
    description: '',//报修描述

  },

  onLoad({ id }) {
    //修改报修
    if (id) {
      this.alter(id)
    }
    this.houseSelect()
    //报修项目
    this.repairSelect()

  },
  //有id就是修改 没有就是添加 
  async alter(id) {
    const res = await wx.http.get(`/repair/${id}`)
    console.log('修改报修详情获取到的id', res);
    //数据回显
    // const { houseId, houseInfo, repairItemId, repairItemName, mobile, appointment, description, status, attachment } = res
    this.setData({
      ...res,
      // houseId, houseInfo, repairItemId, repairItemName, mobile, appointment, description, status, attachment
    })

  },

  //上传附件
  async afterRead(e) {
    // console.log('上传附件', e.detail.file.url);
    const { file } = e.detail
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    const { data } = await wx.utils.upload(file.url)
    console.log('上传附件', data.url);
    // 上传完成需要更新
    const { attachment } = this.data
    attachment.push({ ...data })
    this.setData({ attachment })


  },
  //提交报修
  async goList() {
    //添加报修信息
    const { houseInfo, houseId, mobile, appointment, repairItemId, description, attachment } = this.data
    if (!houseInfo) return wx.showToast({ title: '请选择保修房屋', icon: 'none' })
    if (!repairItemId) return wx.showToast({ title: '请选择报修项目', icon: 'none' })
    if (!mobile) return wx.showToast({ title: '请输入手机号', icon: 'none' })
    if (!appointment) return wx.showToast({ title: '请选择预约时间', icon: 'none' })
    if (!description) return wx.showToast({ title: '请输入报修描述', icon: 'none' })
    // if (!attachment.length) return wx.showToast({ title: '请上传附件', icon: 'none' })
    const res = await wx.http.post('/repair', { houseId, mobile, appointment, repairItemId, description, attachment })
    console.log('添加报修信息', res);
    wx.reLaunch({
      url: `/repair_pkg/pages/list/index?id=${res.id}`,
    })
  },

  //保修房屋选择
  async houseSelect() {
    const res = await wx.http.get('/house')
    this.setData({ houseList: res })
    // console.log('保修房屋选择', res)
  },
  //查询报修项目
  async repairSelect() {
    const res = await wx.http.get('/repairItem')
    this.setData({ repairItem: res })
    // console.log('查询报修项目', res);
  },

  //选中的房屋
  selectHouse(e) {
    const { id, name } = e.detail
    this.setData({ houseId: id, houseInfo: name })
    // console.log('报修房屋选择', id, name);
  },
  //选中的报修项目
  repairItemName(e) {
    const { id, name } = e.detail
    this.setData({ repairItemId: id, repairItemName: name })
    // console.log('报修项目', id, name);
  },
  //选中日期
  appointment(e) {
    // console.log('预约时间', e.detail);
    this.setData({
      appointment: wx.utils.formatTime(e.detail),
      dateLayerVisible: false
    })
  },
  //选择房屋
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  //选择报修项目
  openRepairLayer() {
    this.setData({ repairLayerVisible: true })
  },
  closeRepairLayer() {
    this.setData({ repairLayerVisible: false, })
  },
  //选择时间
  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    console.log('选择时间');
    this.setData({ dateLayerVisible: false })
  },


})
